package com.student.Activitys.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.student.Activitys.R;
import com.student.Activitys.activity.base.BaseActivity;
import com.student.Activitys.adapter.CollegeSurveyAdapter;
import com.student.Activitys.adapter.base.BaseRecyleViewAdapter;
import com.student.Activitys.bean.CollegeSurveyBean;
import com.odd.Activitys.http.manager.HomeManager;
import com.student.Activitys.utils.Constant;
import com.student.Activitys.utils.JumperUtils;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;

/**
 * 项目名称：智慧校园学生端
 * 类描述：学院概况 页面
 * 创建人：张飞祥
 * 创建时间：2017/10/31 10:21
 * 修改人：张飞祥
 * 修改时间：2017/10/31 10:21
 * 修改备注：
 */
public class CollegeSurveyActivity extends BaseActivity {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rv_college_survey)
    RecyclerView rvCollegeSurvey;

    //接收首页 传递过来的 学院概况 数据
    private CollegeSurveyBean bean = null;
    //初始化网络请求
    public HomeManager mApiManager;
    //数据适配器
    private CollegeSurveyAdapter adapter = null;
    @Override
    protected void initUI() {
        setContentView(R.layout.act_college_survey);
        Bundle bundle = this.getIntent().getExtras();    // 获取传递过来的封了数据的Bundle
        // 获取学院概况 实体
        bean = (CollegeSurveyBean) bundle.getSerializable(Constant.COLLEGE_SURVEY_BEAN);
        //网络请求 常驻方法
        mApiManager = mApiManager.getHomeApiManager(CollegeSurveyActivity.this, CollegeSurveyActivity.this.getTaskId());
    }

    @Override
    protected void initData() {
        //开始 http 请求数据
        initHttp();
        //标题
        tvTitle.setText(bean.getTitle());
        //返回键
        ivReturn.setVisibility(View.VISIBLE);
        //退出
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 网络请求
     */
    private void initHttp() {
        //显示dialog
        initShowTimeDialog("正在刷新数据，请稍后...",CollegeSurveyActivity.this);
        //网络请求
        mApiManager.homeCollegeSurvey(Constant.HTTP_HOME_COLLEGE_SURVEY_ACTION,bean.getTitle());
    }

    @Override
    protected void getNotData() {

    }

    /**
     * 首页 学院概况 网络请求 加载是否成功
     */
    @Subscriber(tag = Constant.BUS_HOME_COLLEGE_SURVEY_ON_COMPLETED, mode = ThreadMode.MAIN)
    public void initCollegeSurveyOnCompleted(String str) {
        //销毁handler
        time_handler.removeCallbacks(time_runnable);
        timeDailog.dismiss();
        dialog.dismiss();
    }

    /**
     * 首页 首次请求 学院概况 网络请求 返回具体 数据实体
     */
    @Subscriber(tag = Constant.BUS_HOME_COLLEGE_SURVEY, mode = ThreadMode.MAIN)
    public void initCollegeSurvey(final List<CollegeSurveyBean> list) {
        //填充列表数据
        adapter = new CollegeSurveyAdapter(CollegeSurveyActivity.this, list);
        rvCollegeSurvey.setLayoutManager(new LinearLayoutManager(CollegeSurveyActivity.this, LinearLayoutManager.VERTICAL, false));
        rvCollegeSurvey.setAdapter(adapter);
        //学院概况点击事件
        adapter.setOnItemClickListener(new BaseRecyleViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                /**
                 * 跳转 webView事件
                 */
                Bundle bundle = new Bundle();
                bundle.putString(Constant.WEB_METHOD, Constant.WEB_COLLEGE_SURVEY);           //学院概况 方法名拼接字段
                bundle.putString(Constant.WEB_ID, list.get(position).getId());                //当前学院概况 id
                bundle.putString(Constant.WEB_TITLE, list.get(position).getTitle());          //设置web页面标题
                JumperUtils.JumpTo(CollegeSurveyActivity.this, WebDateActivity.class, bundle);//跳转到下一页面
            }
        });
    }
}
